home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8366 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: wkaufman.us.oracle.com!wkaufman
  2. From: wkaufman@wkaufman.us.oracle.com (William Kaufman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Q] functions returning structures
  5. Date: 3 Mar 1996 18:10:05 GMT
  6. Organization: Oracle Corporation, Redwood Shores CA
  7. Message-ID: <4hcn9u$9fo@inet-nntp-gw-1.us.oracle.com>
  8. References: <4hasjj$opf@decaxp.harvard.edu>
  9. NNTP-Posting-Host: wkaufman.us.oracle.com
  10.  
  11. In article <4hasjj$opf@decaxp.harvard.edu> martino@course2.harvard.edu (Carlo Martino) writes:
  12. ] Is it bad for a function to return a structure?
  13.  
  14.     Not "bad"--it's part of the ANSI Standard.  Just make sure the
  15. structure is defined before the function is defined and declared, and
  16. that the function is properly declared before it's used.
  17.  
  18.     The only problem with structure passing that I know of is that a lot
  19. of pre-ANSI compilers don't accept structure passing (either into or out
  20. of a function).  The standard work-around for that is to pass in the
  21. address of the caller's structure, like,
  22.  
  23.         RGB rgb;
  24.         void foo(/* RGB * */);
  25.  
  26.         foo(&rgb);
  27.  
  28. and have the callee assign through the pointer, like,
  29.  
  30.         void foo(rgbP)
  31.         RGB *rgbP;
  32.         {
  33.             rgbP->r = rgbP->b = 127;
  34.             rgbP->g = 0;
  35.         }
  36.  
  37. ]  I have been programming in
  38. ] C for upwards of 6 years, and only recently did I hear something to the effect
  39. ] that structures returned by functions have a tendency to be corrupted.
  40.  
  41.     I've never seen that happen on a compiler that supported structure
  42. passing.  Maybe the structure wasn't defined or function declared
  43. properly: try turning your compiler's warnings all the way up--maybe
  44. there wasn't a structure definition and the compiler pretended it was an
  45. int,...
  46.  
  47.                                            -- Bill K.
  48.  
  49. Bill Kaufman,          | "Patience is a virtue.  Seersucker is a fabric."
  50. wkaufman@us.oracle.com |                                   -- Bazooka Joe
  51.